import request from '@/utils/request'

enum API {
  // 获取医院的数据 接口
  USERLOGIN_URL = '/front/user/login',
  GETUSERINFO_URL = '/front/user/getInfo',
  USERLOGOUT_URL = '/front/user/logout',
  USERREFRESHTOKEN_URL = '/front/user/refresh_token'
}

type Common<T> = {
  code: string
  data: T
  mesg: string
  time: string
}

// 返回数据基本格式类型
export interface ResponseData {
  status: number
  statusText: string
}
// 用户登录接口返回数据ts类型
export interface UserLoginResponseData extends ResponseData {
  data: CommonResult
}

// 用户登录接口 方式二
export const reqUserLogin2 = (loginInfo: LoginInfo) =>
  request.post<any, UserLoginResponseData>(API.USERLOGIN_URL, `phone=${loginInfo.phone}&password=${loginInfo.password}`)

// 通用返回类型
type CommonResult<T = string> = {
  success: boolean
  state: number
  message: string
  content: T
}

// 用户登录
type LoginInfo = {
  phone: string
  code?: string
  password: string
}
// 用户登录接口 方式一
export const reqUserLogin = (loginInfo: LoginInfo) => {
  return request<CommonResult>({
    method: 'POST',
    url: '/front/user/login',
    // 这里请求数据类型要求 application/x-www-form-urlencoded,需要手动转为字符串；如要求格式json可直接把对象传递
    data: `phone=${loginInfo.phone}&password=${loginInfo.password}`
  })
}

// 获取用户信息 需要认证access_token
type UserInfo = CommonResult<{
  isUpdatePassword: boolean
  portrait: string
  userName: string
}>
// 获取用户信息接口
export const reqUserInfo = () => {
  return request<UserInfo>({
    method: 'GET',
    url: API.GETUSERINFO_URL
  })
}

// 用户退出接口
export const reqUserLogout = () => {
  return request<CommonResult>({
    method: 'POST',
    url: API.USERLOGOUT_URL
  })
}

// 刷新token接口
export const reqUserRefreshToken = (refresh_token: string) => {
  return request<CommonResult>({
    method: 'POST',
    url: API.USERREFRESHTOKEN_URL,
    params: { refreshtoken: refresh_token }
  })
}

// 获取用户列表信息
export type TQueryCondition = Partial<{
  currentPage: number
  pageSize: number
  phone: string
  userId: number
  startCreateTime: string
  endCreateTime: string
}>
export type TQueryResult = {
  current: number
  hitCount: boolean
  optimizeCountSql: boolean
  orders: any[]
  pages: number
  records: TUserItem[]
  searchCount: boolean
  size: number
  total: number
}
export type TUserItem = {
  accountNonExpired: boolean
  accountNonLocked: boolean
  createTime: string
  credentialsNonExpired: boolean
  id: number
  isDel: boolean
  name: string
  password: string
  phone: string
  portrait: string | null
  regIp: string | null
  status: 'ENABLE' | 'DISABLE'
  updateTime: string
}
export const getUserPages = (queryCondition: TQueryCondition = {}) => {
  return request<Common<TQueryResult>>({
    method: 'POST',
    url: '/boss/user/getUserPages',
    data: queryCondition
  })
}

// 启用用户
export const enableUser = (userId: number) => {
  return request<Common<boolean>>({
    method: 'GET',
    url: '/boss/user/enableUser',
    params: { userId }
  })
}

// 禁用用户
export const forbidUser = (userId: number) => {
  return request<Common<boolean>>({
    method: 'POST',
    url: '/boss/user/forbidUser',
    data: { userId }
  })
}

// 列出所有角色 并标志当前用户拥有的角色权限
export interface TUserRole {
  id: number
  hasPermission: boolean
  name: string
}
export const getRolesWithUserPermissions = (userId: number) => {
  return request<Common<TUserRole[]>>({
    method: 'GET',
    url: '/boss/role/getRolesWithUserPermission',
    params: { userId }
  })
}

// 给用户分配角色权限
export const allocateUserRoles = (userId: number, roleIdList: number[]) => {
  return request<Common<boolean>>({
    method: 'POST',
    url: '/boss/role/allocateUserRoles',
    data: { userId, roleIdList }
  })
}
